# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Kyle Keen <keenerd@gmail.com>
# Contributor: Chirantan Ekbote <chirantan.ekbote at gmail.com>
# Contributor: Eric Anderson <ejona86 at gmail.com>
# Contributor: Pierre DOUCET <pierre at equinoxefr.org>

pkgname=openscad
pkgver=2021.01
pkgrel=10
pkgdesc='The programmers solid 3D CAD modeller'
arch=('loong64' 'x86_64')
url='https://openscad.org'
license=('GPL2')
depends=(
  'glibc'
  'gcc-libs'
  'glib2'
  'freetype2'
  'fontconfig'
  'harfbuzz'
  'libx11'
  'libglvnd'
  'glu'
  'gmp'
  'double-conversion'
  'glew'
  'libxml2'
  'cairo'
  'qt5-base'
  'qscintilla-qt5'
  'opencsg'
  'boost-libs'
  'libzip'
  'qt5-multimedia'
  'libspnav'
  'lib3mf'
  'nettle'
  'mpfr'
  'hicolor-icon-theme'
)
makedepends=(
  'git'
  'cgal'
  'eigen'
  'boost'
  'imagemagick'
)
# full tests need imagemagick and an X11 or framebuffer session
# can't find lib3MF from libsavitar?
checkdepends=(
  'cmake'
  'xorgproto'
  'xorg-server-xvfb'
  'python'
)
_commit='41f58fe57c03457a3a8b4dc541ef5654ec3e8c78'
source=(
  "$pkgname::git+https://github.com/openscad/openscad#commit=$_commit"
  "github.com-openscad-MCAD::git+https://github.com/openscad/MCAD"
)
b2sums=('SKIP'
        'SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^openscad-//'
}

prepare() {
  cd "$pkgname"

  # CVE-2022-0496
  git cherry-pick --no-commit 00a4692989c4e2f191525f73f24ad8727bacdf41

  # CVE-2022-0497
  git cherry-pick --no-commit 84addf3c1efbd51d8ff424b7da276400bbfa1a4b

  # undefine boost libintl
  git cherry-pick --no-commit 4c5d19c302d7d440d1c11c3d02b3ce5eb2e548da

  # fix CGAL 5.3
  git cherry-pick --no-commit 9aa0d7e9f2914fe5f547bdde69202161d1c6064d

  # fix boost x32-x64
  git cherry-pick --no-commit c50837d597a17b5d4cd8c7ed8e09cb07edb98a6f

  # fix CGAL 5.4
  git cherry-pick --no-commit abfebc651343909b534ef337aacc7604c99cf0ea

  # fix boost join
  git cherry-pick --no-commit 08bf69b4115c989fc5671254e0d05735d01bcca5

  # prepare git submodule(s) - required for running tests
  git submodule init libraries/MCAD
  git config submodule.libraries/MCAD.url "$srcdir/github.com-openscad-MCAD"
  git -c protocol.file.allow=always submodule update libraries/MCAD
}

build() {
  cd "$pkgname"

  # https://github.com/openscad/openscad/commit/426e71260e2ac178289c30eb1a228912d07c5b16
  export LIB3MF_INCLUDEPATH="/usr/include/lib3mf/Bindings/Cpp"

  # cmake scripts have no install instructions as of ver 2021.01,
  # so let's wait until using cmake for building
  qmake-qt5 PREFIX="/usr"

  make
}

check() {
  cd "$pkgname/tests"

  cmake .

  make

  echo "Running headless tests…"

  xvfb-run ctest \
    -R 'dumptest|csgtexttest|csgtermtest|openscad-nonascii|openscad-override' \
    -E 'astdumptest_allexpressions'
}

package() {
  cd "$pkgname"

  make INSTALL_ROOT="$pkgdir" install

  # desktop file
  install -Dm644 "icons/$pkgname.desktop" "$pkgdir/usr/share/applications/$pkgname.desktop"

  # application icons
  for i in 48 64 128 256 512; do
    install -vDm644 "icons/$pkgname-${i}.png" "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$pkgname.png"
  done

  # add mime info (*.scad)
  install -Dm644 "icons/$pkgname.xml" "$pkgdir/usr/share/mime/packages/$pkgname.xml"

  # remove unnecessary files
  cd "$pkgdir/usr/share/$pkgname"
  rm -rf libraries/MCAD/.git*

  # remove leftover files from tests (must be removed *after* tests are run)
  rm -rf libraries/MCAD/*.py

  # remove bundled ttf-liberation
  rm -rf fonts
}
